import 'dart:async';

import 'package:code_builder/code_builder.dart';
import 'package:macros/macros.dart';
import 'package:my_desk_flutter/macro/impl.dart';
import 'package:dart_style/dart_style.dart';

//自定义repository注解
macro class Repository implements ClassDeclarationsMacro {
  const Repository();

  @override
  FutureOr<void> buildDeclarationsForClass(ClassDeclaration clazz,
      MemberDeclarationBuilder builder) async {
    final generateName = '${clazz.identifier.name}Repository';
    List<MethodDeclaration> methods = await builder.methodsOf(clazz);
    // String code = "";
    final repositoryClass = Class((b) =>
    b
      ..abstract = true
      ..name = generateName
      ..methods.addAll(methods.map((e) {
        return Method((m) {
          m.name = e.identifier.name;
          // m.modifier = M
          // m.returns = ref(e.returnType.code);
        });
      })),
    );

    final emitter = DartEmitter();

    final code = DartFormatter().format(
        repositoryClass.accept(emitter).toString());
    builder.declareInLibrary(await builder.code(code));

    //
    // builder.declareType(
    //     generateName,
    //     await builder.code(code));
  }
}